<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-input
        v-model="listQuery.order_no"
        placeholder="请输入订单编号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.type"
        placeholder="请选择拍品类型"
        clearable
        style="width: 150px; margin-left: 10px"
        class="filter-item"
      >
        <el-option label="首次上拍" :value="0" />
        <el-option label="续拍" :value="1" />
      </el-select>
      <el-input
        v-model="listQuery.product"
        placeholder="请输入拍品名称"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.shopping"
        placeholder="请输入店铺名称"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        icon="iconfont qt-caozuo-xunhuan1"
        @click="clearFilter"
      >
        清除条件
      </el-button>
    </div>

    <!-- <div class="filter-container" style="border: 1px solid #dcdcdc;padding: 20px;margin-bottom: 20px;">
      <el-link :type="tabActive == 0 ? 'primary' : ''" :underline="false" @click="tabChange(0)">全部订单</el-link>
      <el-link :type="tabActive == 1 ? 'primary' : ''" :underline="false" style="margin-left:60px;"
        @click="tabChange(1)">待发货(商家发货)</el-link>
      <el-link :type="tabActive == 2 ? 'primary' : ''" :underline="false" style="margin-left:60px;"
        @click="tabChange(2)">待发货(平台发货)</el-link>
      <el-link :type="tabActive == 3 ? 'primary' : ''" :underline="false" style="margin-left:60px;"
        @click="tabChange(3)">待收货</el-link>
      <el-link :type="tabActive == 4 ? 'primary' : ''" :underline="false" style="margin-left:60px;"
        @click="tabChange(4)">已完成</el-link>
    </div> -->
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="订单编号" prop="order_no" align="center" />
      <el-table-column label="商品id" align="center">
        <template slot-scope="{ row }">
          {{ row.product && row.product.id }}
        </template>
      </el-table-column>
      <el-table-column label="拍品" align="center" width="150">
        <template slot-scope="{ row }">
          <el-image
            v-if="row.product"
            style="width: 40px; height: 40px; tetx-align: center"
            :src="$store.state.settings.imgHost + row.product.image"
            :preview-src-list="[
              $store.state.settings.imgHost + row.product.image,
            ]"
          />
          <span
            v-if="row.product"
            style="margin-left: 10px; vertical-align: top"
          >{{ row.product.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拍品类型" align="center">
        <template v-if="row.product" slot-scope="{ row }">{{
          row.product.type == 0
            ? "首次上拍"
            : row.product.type == 1
              ? "续拍"
              : ""
        }}</template>
      </el-table-column>
      <el-table-column label="账号" prop="user.user" align="center" />
      <el-table-column label="姓名" prop="user.name" align="center" />
      <el-table-column label="出价" align="center" prop="offer" />
      <el-table-column align="center" label="实付" prop="real_pay" />
      <el-table-column align="center" label="状态">
        <template slot-scope="{ row }">
          {{
            row.status == 0
              ? "待支付"
              : row.status == 1
                ? "待发货"
                : row.status == 2
                  ? "待收货"
                  : row.status == 3
                    ? "已完成"
                    : row.status == 4
                      ? "锁定期"
                      : row.status == 5
                        ? "拍卖中-首次上架"
                        : row.status == 6
                          ? "拍卖中-流拍"
                          : row.status == 7
                            ? "拍卖中-支付逾期"
                            : row.status == 8
                              ? "拍卖中-重新上架"
                              : row.status == 9
                                ? "已成交"
                                : row.status == 10
                                  ? "退款完成"
                                  : row.status == 11
                                    ? "退货"
                                    : row.status == 12
                                      ? "退货中"
                                      : ""
          }}
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="店铺名称">
        <template slot-scope="{ row }">
          {{
            row.type == 0
              ? seller_shopping.name
              : row.type == 1
                ? seller_shopping.shopping_name
                : ""
          }}
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="付款时间" prop="pay_success_at" />
      <el-table-column align="center" label="配送方式" prop="express_way" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status == 1"
            size="mini"
            plain
            type="success"
            @click="getKdData(row.id)"
          >
            立即发货
          </el-button>
          <el-button
            size="mini"
            plain
            type="primary"
            @click="handleLook(row.id)"
          >
            订单详情
          </el-button>
          <el-button
            v-if="row.status == 2"
            size="mini"
            plain
            type="warning"
            @click="handleAccept(row.id)"
          >
            确认收货
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>

    <!-- 发货 -->
    <el-dialog title="立即发货" :visible.sync="dialogVisible" width="40%">
      <el-form
        ref="postForm"
        :model="form"
        :rules="rules"
        class="form-container"
      >
        <div class="createPost-main-container">
          <el-form-item label-width="90px" label="物流公司:" prop="express_way">
            <el-select
              v-model="form.express_way"
              placeholder="请选择物流公司"
              clearable
              style="width: 100%"
              class="filter-item"
            >
              <el-option
                v-for="(item, index) in kdList"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label-width="90px" label="快递单号:" prop="express_no">
            <el-input v-model.trim="form.express_no" />
          </el-form-item>
        </div>
      </el-form>
      <div style="margin-top: 40px; text-align: center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="danger" @click="submitHandle">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrdersList, getKdCompanyList, updateOrders } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'OrdersDelivery',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      dialogVisible: false,
      form: {
        status: 2
      },
      params_id: undefined,
      tabActive: 0,
      kdList: undefined,
      listQuery: {
        page: 1,
        per_page: 20,
        order_no: undefined,
        product: undefined,
        shopping: undefined,
        type: 0,
        order_type: 1
      },
      rules: {
        express_way: [
          {
            required: true,
            message: '请选择物流公司',
            trigger: 'change'
          }
        ],
        express_no: [
          {
            required: true,
            message: '请输入快递单号',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['order_no'] = undefined
      this.listQuery['product'] = undefined
      this.listQuery['shopping'] = undefined
      this.listQuery['type'] = 0
      this.handleFilter()
    },
    tabChange(e) {
      this.tabActive = e
    },
    submitHandle() {
      updateOrders(this.params_id, this.form).then(() => {
        this.$notify({
          title: '提示',
          message: '发货成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    getKdData(id) {
      this.dialogVisible = true
      this.params_id = id
      getKdCompanyList().then((res) => {
        if (res.code == '200') {
          //   let array = (res.data || []).map((row,index)=>{
          //       return {
          //       value: index,
          //       key: row
          //     }
          //   })
          this.kdList = res.data || []
        }
      })
    },
    getList() {
      this.listLoading = true
      getOrdersList(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleAccept(id) {
      this.$confirm('手动确认收货：确保买家已经收到您的商品', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateOrders(id, {
            status: 3
          }).then(() => {
            this.$notify({
              title: '提示',
              message: '收货成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
        .catch(() => {})
    },
    // 查看
    handleLook(id) {
      this.$router.push('/orders/detail-th/' + id)
    }
  }
}
</script>
